#include <iostream>
#include <chrono>
#include <unistd.h>
#include <thread>
#include <cstdlib>

#include "listed_timer.h"

using namespace std;

bool stop = false;
int main() {

    ListedTimer::SortedTimers timers;

//    auto trigger = std::thread([](){
//        std::this_thread::sleep_for(chrono::seconds(100));
//        stop = true;
//    });

//    auto runner = std::thread([&](){
//        while (!stop) {
//            timers.tick();
//        }
//    });

//    for (int i = 0; i < 10; ++i) {
//        timers.add(i, 5, [=](){
//            cout << "task no." << i + 1 << endl;
//        });
//
//        unsigned int seed = chrono::steady_clock::now().time_since_epoch().count() + i;
//        unsigned int sec = rand_r(&seed) % 10 + 1;
//        cout << sec << endl;
//        this_thread::sleep_for(chrono::seconds(sec));
//    }


//    this_thread::sleep_for(chrono::seconds(int(10 * 60)));
//    stop = true;
//    runner.join();
    int step = 0;
    while(!stop) {
        int time_slot = timers.get_next_tick();

        if (step == 0) {
            this_thread::sleep_for(chrono::seconds(time_slot));
            timers.add(8, 10, [&](){
                timers.erase(8);
                cout << "delete timer." << endl;
            });
            step++;
            continue;
        } else if (step == 1){
            this_thread::sleep_for(chrono::seconds(2));
            timers.adjust(8, 10);
            cout << "adjust timer." << endl;
            step++;
            continue;
        }

    }

    return 0;
}